package com.jianguozh.difs.support.service.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianguozh.difs.framework.constant.enums.ClientEnum;
import com.jianguozh.difs.support.service.component.prototypeparam.Group;
import com.jianguozh.difs.support.service.component.prototypeparam.PrototypeParam;
import com.jianguozh.difs.support.service.component.prototypeparam.PrototypeParamManager;
import com.jianguozh.difs.support.service.base.entity.Param;
import com.jianguozh.difs.support.service.base.mapper.ParamMapper;
import com.jianguozh.difs.support.service.base.service.IParamService;
import com.jianguozh.difs.support.service.base.vo.GroupVo;
import com.jianguozh.difs.support.service.base.vo.ParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * <p>
 * 参数配置 服务实现类
 * </p>
 *
 * @author hanrs
 * @since 2022-03-17
 */
@Service
public class ParamServiceImpl extends ServiceImpl<ParamMapper, Param> implements IParamService {

    @Autowired
    private PrototypeParamManager prototypeParamManager;

    @Override
    public Param getByKey(String paramKey, String actAtOrgId) {
        LambdaQueryWrapper<Param> lambdaQueryWrapper = new LambdaQueryWrapper<Param>().eq(Param::getParamKey, paramKey);
        if (StrUtil.isNotBlank(actAtOrgId)) {
            lambdaQueryWrapper.eq(Param::getActAtOrgId, actAtOrgId);
        } else {
            lambdaQueryWrapper.isNull(Param::getActAtOrgId);
        }
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }


    @Override
    public ParamVo getVoByKey(String paramKey, String actOrgId) {
        PrototypeParam prototypeParam = prototypeParamManager.getParamByKey(paramKey);
        if (null == prototypeParam) {
            return null;
        }

        ParamVo paramVo = new ParamVo();
        BeanUtil.copyProperties(prototypeParam, paramVo);
        Param param = getByKey(paramKey, actOrgId);
        if (null != param) {
            paramVo.setParamValue(param.getParamValue());
        } else {
            paramVo.setParamValue(paramVo.getParamDefaultValue());
        }
        return paramVo;
    }

    @Override
    public PrototypeParam getPrototypeByKey(String paramKey) {
        return prototypeParamManager.getParamByKey(paramKey);
    }

    @Override
    public Set<GroupVo> getGroupVos(ClientEnum clientEnum, String actOrgId) {
        Set<GroupVo> groupVos = new HashSet<>();
        Set<Group> set = prototypeParamManager.getGroupSetByClient(clientEnum);
        Iterator<Group> it = set.iterator();
        while (it.hasNext()) {
            Group group = it.next();
            GroupVo groupVo = getGroupVo(group.getGroupKey(), actOrgId);
            groupVos.add(groupVo);
        }
        return groupVos;
    }

    @Override
    public GroupVo getGroupVo(String groupKey, String actOrgId) {
        Group group = prototypeParamManager.getGroupByKey(groupKey);
        Set<ParamVo> paramVos = new HashSet<>();
        group.getPrototypeParams().forEach(p -> {
            ParamVo paramVo = getVoByKey(p.getParamKey(), actOrgId);
            paramVos.add(paramVo);
        });
        GroupVo groupVo = new GroupVo();
        BeanUtil.copyProperties(group, groupVo);
        groupVo.setParamVos(paramVos);
        return groupVo;
    }
}
